package com.gugee.ins.data.analysis.service.impl;
import com.gugee.common.thread.ScheduleTemplate;

import com.gugee.ins.common.constant.RedisKeyConst;
import com.gugee.ins.data.analysis.config.InsAnalysisConfig;
import com.gugee.ins.data.analysis.service.InsAnalysisAccountNormalService;
import com.gugee.ins.data.common.msg.InsBloggerMessage;
import com.xiguaji.boot.toolkit.ThreadPoolUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

/**
 * InsAnalysisAccountNormalImpl 服务
 * @author Huangxin
 */
@Slf4j
@Service
public class InsAnalysisAccountNormalImpl implements InsAnalysisAccountNormalService {

    /**
     * 配置
     */
    @Autowired
    InsAnalysisConfig insAnalysisConfig;

    /**
     * 线程调度
     */
    @Autowired
    ScheduleTemplate scheduleTemplate;

    /**
     * redis
     */
    @Autowired
    RedisTemplate<String,String> redisTemplate;


    /**
     * 线程池
     */
    ForkJoinPool forkJoinPool;

    /**
     * 线程名
     */
    private String workName = "insAnalysisAccountNormal-worker";

    /**
     * 初始化线程池
     */
    @PostConstruct
    public void init() {
        forkJoinPool = ThreadPoolUtils.forkJoinPool(insAnalysisConfig.getInsAnalysisAccountNormalCorePollSize(), workName);
        scheduleTemplate.attach(workName, forkJoinPool);
    }

    /**
     * 处理 成功 调用uid接口的情况
     * @param insBloggerMessages
     */
    @Override
    public void handleAnalysisAccountNormal(List<InsBloggerMessage> insBloggerMessages) {
        try {
            forkJoinPool.submit(() -> insBloggerMessages.parallelStream().forEach(msg -> {
                try {
                    analysisAccountNormal(msg.getUid());
                } catch (Exception ex) {
                    log.error("insAnalysisAccountNormal error. uid:{}, error :{}" ,
                            msg.getUid(),ex.getMessage());
                }
            })).get();
        } catch (Exception ex) {
            log.error("insAnalysisAccountNormal thread error.  {}" ,  ex.getMessage());
        }
    }

    /**
     * 处理 成功 调用uid接口的情况
     *
     * @param uid
     */
    @Override
    public void analysisAccountNormal(Long uid){
        long start = System.currentTimeMillis();
        if(uid != null){
            // redis key
            String key = RedisKeyConst.INS_SPIDER_UNUSUAL + uid;
            // 判断 redis 里是否有 该uid 有则删除
            String value = redisTemplate.opsForValue().get(key);

            if(!StringUtils.isEmpty(value)){
                redisTemplate.delete(key);
            }
        }
        if ((System.currentTimeMillis() - start) / 1000 > insAnalysisConfig.getInsAnalysisDebugTimeLimit()) {
            log.info("insAnalysisAccountNormal uid:{} use time:{}s", uid,(System.currentTimeMillis() - start) / 1000);
        }
    }
}
